package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.service.WechatService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WechatServiceImpl implements WechatService {

    //获取OpenID
    public static final String URL_OPENID="https://api.weixin.qq.com/sns/jscode2session";

    //获取AccessToken地址
    public static final String URL_ACCESSTOKEN="https://api.weixin.qq.com/cgi-bin/token";

    //获取手机号地址
    public static final String URL_PHONE="https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    @Value("${wechat.appId}")
    private String appId;

    @Value("${wechat.appSecret}")
    private String appSecret;

    @Override
    public String getOpenId(String code) {
        //1、封装请求参数
        Map<String, Object> dataMap = getConfigApi();
        dataMap.put("js_code",code);
        dataMap.put("grant_type","authorization_code");

        //2、使用Hutool的HttpUtil发送Get请求
        String result = HttpUtil.get(URL_OPENID, dataMap);

        //3、获取响应结果，并解析结果
        JSONObject jsonObject = JSONUtil.toBean(result, JSONObject.class);

        //3.1 errcode=0，表示没有错，可以获取openid
        String openId = jsonObject.getString("openid");
        if(ObjectUtil.isEmpty(openId)){
            throw new BaseException("获取OpenID异常！");
        }
        return openId;
    }

    @NotNull
    private Map<String, Object> getConfigApi() {
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("appid",appId);
        dataMap.put("secret",appSecret);
        return dataMap;
    }

    @Override
    public String getPhone(String code) {
        //1、定义参数
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("code",code);

        //2、执行POST请求
        HttpResponse response = HttpUtil.createPost(URL_PHONE + getAccessToken())
                .body(JSONUtil.toJsonStr(dataMap), "application/json;charset=utf-8")
                .execute();

        //3、解析数据，获取手机号
        String result = response.body();
        JSONObject jsonObject = JSONUtil.toBean(result, JSONObject.class);
        if(jsonObject.getInteger("errcode")!=0){
            throw new BaseException("获取手机号失败！");
        }
        Object phone = jsonObject.getByPath("phone_info.purePhoneNumber");
        return phone.toString();
    }


    /****
     * 获取accessToken
     */
    public String getAccessToken(){
        //1、定义参数
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("grant_type","client_credential");
        dataMap.put("appid","wx19aaa6e7b0852b88");
        dataMap.put("secret","587e7ce7443b8fbe0362153bea92352e");

        //2、执行GET请求
        String result = HttpUtil.get(URL_ACCESSTOKEN, dataMap);

        //3、解析数据
        JSONObject jsonObject = JSONUtil.toBean(result,JSONObject.class);
        String accessToken = jsonObject.getString("access_token");
        if(ObjectUtil.isEmpty(accessToken)){
            throw new BaseException("获取AccessToken失败！");
        }
        return accessToken;
    }


}
